package com.android.lovingpet.signup.view;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.signup.ILoginContract;
import com.android.lovingpet.signup.LoginInPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by ikata on 2018/1/16.
 */
public class LoginInContentfragment extends BaseFragment<ILoginContract.View, LoginInPresenter> implements View.OnClickListener, ILoginContract.View {

    TextView tvAgreement;
    private ImageView im_password;
    private EditText et_phone;
    private EditText et_password;
    private CheckBox cbTeam;
    private boolean isShow = false;
    @Override
    protected int setContentView() {
        return R.layout.fragment_login_in_content;
    }

    @Override
    protected void onCreateView(View view) {
        tvAgreement=view.findViewById(R.id.tv_agreement);
        tvAgreement.setOnClickListener(this);
        ((TextView) view.findViewById(R.id.tv_title)).setText("注册");
        Button bt_post_verificationcode = view.findViewById(R.id.login_in_next1);
        //发送验证码
        bt_post_verificationcode.setOnClickListener(this);

        im_password =view.findViewById(R.id.login_in_show_password);
        et_password = view.findViewById(R.id.login_in_password);
        et_phone = view.findViewById(R.id.login_in_phone);
        cbTeam = view.findViewById(R.id.cb_team);
        //字体下划线
        tvAgreement.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        tvAgreement.getPaint().setAntiAlias(true);
        im_password.setOnClickListener(this);
    }

    @Override
    public LoginInPresenter createPresenter() {
        return ((LoginInActivity) getActivity()).getPresenter();
    }

    @Override
    public ILoginContract.View createView() {
        return this;
    }


    @Override
    public void onResume() {
        super.onResume();
        reBind();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_in_show_password:
                //密码显示
                displayPassword();
                break;

            case R.id.login_in_next1:
                String phone = et_phone.getText().toString();
                String password = et_password.getText().toString();
                getPresenter().next(phone, password, cbTeam.isChecked() ? 0 : 1);
                break;
            case R.id.tv_agreement:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.login_in_count,new AgreementFragment()).commit();
                break;
            default:
                break;
        }
    }

    @Override
    public void next() {
        getFragmentManager().beginTransaction().addToBackStack(null).
                replace(R.id.login_in_count, new LoginInYzmFragment()).commit();
    }

    private void displayPassword() {
        if (isShow) {
            et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
            et_password.setSelection(et_password.getText().length());
            im_password.setBackground(getContext().getResources().getDrawable(R.drawable.image_showpassword));
            isShow = false;
        } else {
            et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            et_password.setSelection(et_password.getText().length());
            im_password.setBackground(getContext().getResources().getDrawable(R.drawable.image_noshowpassword));
            isShow = true;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
